﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
namespace AutumnEngine.Editor
{
    public partial class MainForm : RibbonForm
    {
        const bool black = true;
        public EditorGame game;
        public PropertyForm props = new PropertyForm();
        Size origSize;
        Thread origSizer;
        bool addingTexture = false;
        bool addingFont = false;
        List<RibbonButton> buttons = new List<RibbonButton>();
        List<RibbonButton> fontButtons = new List<RibbonButton>();

        public void NodeAdded(string name)
        {
            this.Invoke(new ThreadStart(delegate
            {
                props.treeView1.Nodes.Add(name);

            }));
        }

        public void AddDefaultAssetsAndClear()
        {
            //We add blank and default fonts to the editor list.
            buttons.Clear();
            textures.DropDownItems.Clear();
            fonts.DropDownItems.Clear();
            //Texture
            RibbonButton add = new RibbonButton();
            add.Text = "Blank";
            textures.DropDownItems.Add(add);
            AddButton(add);
            textures.TextBoxText = "Blank";
            //
            //Font
            RibbonButton adds = new RibbonButton();
            adds.Text = "Engine";
            fonts.DropDownItems.Add(adds);
            AddFont(adds);
            fonts.TextBoxText = "Engine";
            //
        }

        public MainForm()
        {
            InitializeComponent();
            if (black)
            {
                //Apply black renderer to ribbon1
                (ribbon1.Renderer as RibbonProfessionalRenderer).ColorTable =
                      new System.Windows.Forms.RibbonProfesionalRendererColorTableBlack();
            }

            //Create the game
            game = new EditorGame();
            game.PreViewerAdd(xnaViewer1.Size.Width, xnaViewer1.Size.Height);
            props.Show();
            origSizer = new Thread(new ThreadStart(delegate
                {
                    Thread.Sleep(100);
                    origSize = this.Size;
                }));
            origSizer.Start();
        }

        private void ribbonOrbMenuItem3_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            //Create the game
            xnaViewer1.MouseLeave += new EventHandler(xnaViewer1_MouseLeave);
            xnaViewer1.MouseEnter += new EventHandler(xnaViewer1_MouseEnter);
            xnaViewer1.RunGame(game);
            game.IsEnabled = false;
            game.Type = NodeType.Sprite;
            origSize = this.Size;
            AddDefaultAssetsAndClear();
        }

        public void SetSelectedNode(string name)
        {
            game.SelectedNodeName = name;
        }

        public void DeletedNode(string name)
        {
          
            if (props.treeView1.Nodes.Count == 1)
            {                
                props.propertyGrid1.SelectedObject = null;
                props.treeView1.SelectedNode = null;
                props.treeView1.Nodes.Clear();
                return;
            }
            else
            {
                if (props.treeView1.SelectedNode == props.treeView1.Nodes[0])
                {
                    props.treeView1.SelectedNode = props.treeView1.Nodes[1];
                    props.treeView1.Nodes.RemoveAt(0);
                }
                props.treeView1.SelectedNode = props.treeView1.Nodes[0];
            }

            foreach (TreeNode n in props.treeView1.Nodes)
            {
                if (n.Text == name)
                {
                    props.treeView1.Nodes.Remove(n);
                }
            }
        }

        void xnaViewer1_MouseEnter(object sender, EventArgs e)
        {
            game.IsEnabled = true;
        }

        void xnaViewer1_MouseLeave(object sender, EventArgs e)
        {
            game.IsEnabled = false;
        }

        public void ChangeSelected()
        {
            this.Invoke(new ThreadStart(delegate
            {
                props.propertyGrid1.SelectedObject = game.SelectedNode;
            }));
        }        

        private void ribbonOrbMenuItem1_Click(object sender, EventArgs e)
        {
            SaveScene();
        }

        private void ribbonOrbMenuItem2_Click(object sender, EventArgs e)
        {
            LoadScene();
        }

        private void newButton_Click(object sender, EventArgs e)
        {
            this.newScene();
        }

        void newScene()
        {
            props.propertyGrid1.SelectedObject = null;
            props.treeView1.Nodes.Clear();
            game.DeleteAllNodes();
            AddDefaultAssetsAndClear();
            textures.TextBoxText = "Blank";
            fonts.TextBoxText = "Engine";
            game.SetNewTexture("Blank");
            game.SetNewFont("Engine");
        }

        void SaveScene()
        {
            SaveFileDialog save = new SaveFileDialog();
            save.RestoreDirectory = true;
            save.AddExtension = true;
            save.AutoUpgradeEnabled = true;
            save.DefaultExt = "asf";
            save.Filter = Engine.Modules.Settings.EngineName + " Scene Files (*.asf)|*.asf";
            string path = Directory.GetCurrentDirectory() + "/Content/Scenes";
            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);
            save.Title = "Save Scene...";
            save.InitialDirectory = path;
            DialogResult res = save.ShowDialog();
            if (res == DialogResult.OK)
            {
                game.Save(save.FileName);
            }
        }

        void AddButton(RibbonButton b)
        {
            buttons.Add(b);
            b.Click += new EventHandler(b_Click);
        }

        void b_Click(object sender, EventArgs e)
        {
            string textureName = (((RibbonButton)(sender)).Text);
            textures.TextBoxText = textureName;
            game.SetNewTexture(textureName);
        }

        void AddFont(RibbonButton f)
        {
            buttons.Add(f);
            f.Click += new EventHandler(f_Click);
        }

        void f_Click(object sender, EventArgs e)
        {
            string fontName = (((RibbonButton)(sender)).Text);
            fonts.TextBoxText = fontName;
            game.SetNewFont(fontName);
        }

        void LoadScene()
        {
            OpenFileDialog open = new OpenFileDialog();
            open.RestoreDirectory = true;
            open.AddExtension = true;
            open.AutoUpgradeEnabled = true;
            open.DefaultExt = "asf";
            open.Filter = Engine.Modules.Settings.EngineName + " Scene Files (*.asf)|*.asf";
            string path = Directory.GetCurrentDirectory() + "/Content/Scenes";
            if (!Directory.Exists(path))
                Directory.CreateDirectory(path);
            open.Title = "Open Scene...";
            open.InitialDirectory = path;
            DialogResult res = open.ShowDialog();
            if (res == DialogResult.OK)
            {
                AddDefaultAssetsAndClear();
                textures.TextBoxText = "Blank";
                fonts.TextBoxText = "Engine";
                game.SetNewTexture("Blank");
                game.SetNewFont("Engine");
                game.Load(open.FileName);
            }

            populateTreeView();
        }

        void populateTreeView()
        {
            props.treeView1.Nodes.Clear();
            foreach (string name in game.GameScene.Nodes.Keys)
            {
                props.treeView1.Nodes.Add(name);
            }
        }

        private void ribbonPanel6_Click(object sender, EventArgs e)
        {
            game.Type = NodeType.Label;
        }

        private void ribbonSaveButton_Click(object sender, EventArgs e)
        {
            SaveScene();
        }

        private void ribbonOpenButton_Click(object sender, EventArgs e)
        {
            LoadScene();
        }

        private void spriteNodeButton_Click(object sender, EventArgs e)
        {
            game.Type = NodeType.Sprite;
        }

        private void labelButton_Click(object sender, EventArgs e)
        {
            game.Type = NodeType.Label;
        }
       

        private void markerButton_Click(object sender, EventArgs e)
        {
            game.Type = NodeType.Marker;
        }

        private void MainForm_SizeChanged(object sender, EventArgs e)
        {
            if (origSize != null)
                this.Size = origSize;
        }        

        private void MainForm_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            this.MaximumSize = this.Size;
        }

        private void ribbonButton3_Click(object sender, EventArgs e)
        {
            game.DeleteSelectedNode();
        }

        private void ribbonButton2_Click(object sender, EventArgs e)
        {
            game.Type = NodeType.Sprite;
        }       

        private void ribbonButton4_Click(object sender, EventArgs e)
        {
            game.Type = NodeType.Physic;
        }

        private void ribbonButton5_Click(object sender, EventArgs e)
        {
            game.Type = NodeType.Label;
        }

        private void ribbonButton6_Click(object sender, EventArgs e)
        {
            game.Type = NodeType.Marker;
        }

        private void addButton_Click(object sender, EventArgs e)
        {
            AssetAddForm add = new AssetAddForm(AssetType.Texture);
            add.Show();
            addingTexture = true;
        }

        public void finishedAdding(AssetType type,string name)
        {
            switch (type)
            {
                case AssetType.Texture:
                    {
                        addingTexture = false;
                        RibbonButton add = new RibbonButton();
                        add.Text = name;
                        textures.DropDownItems.Add(add);
                        AddButton(add);
                        break;
                    }
                case AssetType.Font:
                    {
                        addingFont = false;
                        RibbonButton add = new RibbonButton();
                        add.Text = name;
                        fonts.DropDownItems.Add(add);
                        AddFont(add);
                        break;
                    }
            }
        }

        private void textures_TextBoxTextChanged(object sender, EventArgs e)
        {
        }

        private void ribbonButton7_Click(object sender, EventArgs e)
        {
            AssetAddForm add = new AssetAddForm(AssetType.Font);
            add.Show();
            addingFont = true;
        }
    }
}
